<?php

$http = new swoole_http_server("0.0.0.0", 9501);

$http->set([
    'document_root' => '/home/service/web/tpswoole/public/static',
    'enable_static_handler' => true,
    'worker_num' => 5,
]);

$http->on('WorkerStart', function (swoole_server $server, $worker_id) {
    define('APP_PATH', __DIR__ . '/../application/');
    require __DIR__ . '/../thinkphp/base.php';
});

$http->on('request', function ($request, $response) use($http) {

    if (isset($request->server)){
        foreach ($request->server as $k => $v){
            $_SERVER[strtoupper($k)] = $v;
        }
    }

    if (isset($request->header)){
        foreach ($request->header as $k => $v){
            $_SERVER[strtoupper($k)] = $v;
        }
    }
    $_GET = [];
    if (isset($request->get)){
        foreach ($request->get as $k => $v){
            $_GET[$k] = $v;
        }
    }
    $_POST = [];
    if (isset($request->post)){
        foreach ($request->post as $k => $v){
            $_POST[$k] = $v;
        }
    }

//    echo request()->action().PHP_EOL;

    ob_start();
    try{
        \think\App::run()->send();
    }catch (Exception $e){

    }
//    echo '--action--'.request()->action().PHP_EOL;
    $res = ob_get_contents();
    ob_end_clean();

    $response->end($res);
//    $http->close();
});

$http->start();